package com.mayikt;

import java.util.concurrent.Semaphore;

/**
 * @ClassName Test033
 * @Author 蚂蚁课堂余胜军 QQ644064779 www.mayikt.com
 * @Version V1.0
 **/
public class Test033 {
    public static void main(String[] args) {
        Semaphore semaphore = new Semaphore(5);
        for (int i = 0; i < 6; i++) {
            new Thread(() -> {
                try {
                    // 获取凭证 状态-1
                    semaphore.acquire();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println(Thread.currentThread().getName() + ",抢到了凭据");
                try {
                    Thread.sleep(1000);
                    System.out.println(Thread.currentThread().getName() + ",释放凭据");
                    // 状态+1
                    semaphore.release();
                } catch (Exception e) {

                }

            }).start();
        }


    }
}
